package com.xp.pgojcodesandbox.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.ListContainersCmd;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.core.DockerClientBuilder;

import java.util.List;

/**
 * @author <a href=""https://github.com/Fzs0>逢左使</a>
 * @create 2024/06/17 22:10
 * @Description:
 */
public class DockerDemo {

    public static void main(String[] args) throws InterruptedException {
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();
        String image = "hello-world:latest";
        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(image);
        CreateContainerResponse createContainerResponse = containerCmd.exec();
        // 创建的容器id
        String containId = createContainerResponse.getId();
        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
        List<Container> containerList =
                listContainersCmd.withShowAll(true).exec();
        for (Container container : containerList) {
            System.out.println(container);
        }
        // 启动容器
        dockerClient.startContainerCmd(containId).exec();

    }

}
